import { config } from "./framework/config";
import app from "./app";
import { logger } from './framework/config/logger'; // 导入日志配置
const PORT = config.port;

const server = app.listen(PORT, () => {
  logger.info(`运行环境: ${config.env}`);
  logger.info(`服务器运行在端口 ${PORT}`);
  logger.info(`健康检查: http://localhost:${PORT}/api/health`);
  logger.info(`版本检查: http://localhost:${PORT}/api/version`);
});

// 优雅关闭
process.on("SIGTERM", () => {
  logger.info("SIGTERM received, shutting down gracefully");
  server.close(() => {
    logger.info("Process terminated");
  });
});

// 未捕获的异常处理
process.on('uncaughtException', (error) => {
  logger.error('Uncaught Exception:', error);
  process.exit(1);
});

// 未处理的 Promise 拒绝处理
process.on('unhandledRejection', (reason, promise) => {
  logger.error('Unhandled Rejection at:', promise, 'reason:', reason);
  process.exit(1);
});